package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessFarmSystem;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.LogisticsVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.LogisticsHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/8/11 11:04
 * 修改备注：
 * ================================================
 */
public class LogisticsQueryActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.logistics_tv_name)
    TextView logisticsTvName;
    @Bind(R.id.logistics_tv_number)
    TextView logisticsTvNumber;
    @Bind(R.id.logistics_tv_null)
    TextView logisticsTvNull;
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private String orderNo;
    private LogisticsVO logisticsVO;


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_logistics_query);
        setTitle("物流查询");
    }

    @Override
    public void initMember() {

        orderNo = (String) getIntent().getSerializableExtra("orderNo");


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, false, true, false));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new LogisticsHolder(parent);
            }
        });

        onRefresh();

    }

    @Override
    public void addListener() {

        recyclerView.setRefreshListener(this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_LOGISTICS_INFO_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_LOGISTICS_INFO_FIELD:
                        showToast(msg.obj.toString());
                        recyclerView.showEmpty();
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        logisticsVO = FastJsonUtil.getBean(object.toString(), "logistics", LogisticsVO.class);

        if (logisticsVO == null)
            return;

        logisticsTvName.setText(logisticsVO.getDeliveryCorp());
        logisticsTvNumber.setText(logisticsVO.getTrackingNo());

        if (logisticsVO.getLogisticsInfos() != null) {
            adapter.clear();
            adapter.addAll(logisticsVO.getLogisticsInfos());
        } else {
            logisticsTvNull.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);

            if(!StringUtil.isStrEmpty(logisticsVO.getLogisticsInfoString()))
                logisticsTvNull.setText(logisticsVO.getLogisticsInfoString());
        }


    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        BusinessFarmSystem.getLogisticsInfo(this, orderNo, mHandler);
    }

}
